package org.korosoft.findbugs.aggregator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.*;

public final class BugCollector {
    private static final Log LOG = LogFactory.getLog(BugCollector.class);

    private final Set<Bug> allBugs = new HashSet<Bug>();

    private final Map<String, List<Bug>> byType = new HashMap<String, List<Bug>>();
    private final Map<String, List<Bug>> byPriority = new HashMap<String, List<Bug>>();
    private final Map<String, List<Bug>> byCategory = new HashMap<String, List<Bug>>();

    public void addBug(Bug bug) {
        LOG.debug("Found bug of type " + bug.getType());

        if (allBugs.add(bug)) {
            if (byType.get(bug.getType()) == null) {
                byType.put(bug.getType(), new ArrayList<Bug>());
            }
            byType.get(bug.getType()).add(bug);

            if (byPriority.get(bug.getPriority()) == null) {
                byPriority.put(bug.getPriority(), new ArrayList<Bug>());
            }
            byPriority.get(bug.getPriority()).add(bug);

            if (byCategory.get(bug.getCategory()) == null) {
                byCategory.put(bug.getCategory(), new ArrayList<Bug>());
            }
            byCategory.get(bug.getCategory()).add(bug);
        }
    }

    public Map<String, List<Bug>> getByType() {
        return byType;
    }

    public Map<String, List<Bug>> getByPriority() {
        return byPriority;
    }

    public Map<String, List<Bug>> getByCategory() {
        return byCategory;
    }

    public int getCount() {
        return allBugs.size();
    }

    public Set<Bug> getAllBugs() {
        return allBugs;
    }
}
